package info.webest.media.headset;

import info.webest.media.activities.MainActivity;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.os.Handler;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.Toast;

public class HeadsetButtonReceiver extends BroadcastReceiver {

	private static final int DOUBLE_CLICK_DELAY = 2000;
	public static int clickCount = 0;

	public static void setRegister(boolean register) {
		AudioManager audioManager = (AudioManager) MainActivity.getInstance()
				.getSystemService(android.content.Context.AUDIO_SERVICE);

		if (register) {
			// ON Headset Button
			audioManager.registerMediaButtonEventReceiver(new ComponentName(
					MainActivity.getInstance().getPackageName(),
					HeadsetButtonReceiver.class.getName()));
		} else {
			// OFF Headset Button
			audioManager.unregisterMediaButtonEventReceiver(new ComponentName(
					MainActivity.getInstance().getPackageName(),
					HeadsetButtonReceiver.class.getName()));

		}

	}

	@Override
	public void onReceive(final Context context, Intent intent) {

		String action = intent.getAction();

		if (!Intent.ACTION_MEDIA_BUTTON.equals(action)) {
			return;
		}

		KeyEvent event = (KeyEvent) intent
				.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
		if (event == null) {
			return;
		}

		// event.getKeyCode() == KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE
		// KEYCODE_HEADSETHOOK

		if (event.getAction() == KeyEvent.ACTION_MULTIPLE) {
			Toast.makeText(context, "HEADSET ACTION_MULTIPLE!!!!!!!!!",
					Toast.LENGTH_SHORT).show();
		}

		if (event.getAction() == KeyEvent.ACTION_DOWN) {
			clickCount++;

			Log.d("getRepeatCount", "" + event.getRepeatCount());

			if (clickCount == 1) {
				Handler handler = new Handler();
				Runnable r = new Runnable() {
					@Override
					public void run() {
						if (clickCount == 1) {
							// if (Options.HeadsetButton) {
							MainActivity.getInstance().toggle();
							// }
							Toast.makeText(context, "HEADSET BUTTON PRESSED!",
									Toast.LENGTH_SHORT).show();

						} else {
							MainActivity.getInstance().skip();
							Toast.makeText(context,
									"HEADSET BUTTON PRESSED DOUBLE!",
									Toast.LENGTH_SHORT).show();
						}
						clickCount = 0;
					}
				};
				handler.postDelayed(r, DOUBLE_CLICK_DELAY);
			} 
			Log.d("HEADSET BUTTON PRESSED", "clickCount: " + clickCount);
		}
		//abortBroadcast();
	}

	// <receiver android:name=".HeadsetButtonReceiver">
	// <intent-filter>
	// <action android:name="android.intent.action.MEDIA_BUTTON" />
	// </intent-filter>
	// NB: this is instead of calling registerReceiver.

	// ((AudioManager)getSystemService(AUDIO_SERVICE)).registerMediaButtonEventReceiver(
	// new ComponentName(
	// getPackageName(),
	// MediaButtonIntentReceiever.class.getName()));
}